package cn.meye11.crm.controller;

import cn.meye11.crm.common.RestResponse;
import cn.meye11.crm.domain.model.ConsumptionRecords;
import cn.meye11.crm.domain.query.ConsumptionRecordsQuery;
import cn.meye11.crm.service.ConsumptionRecordsService;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消费记录表(ConsumptionRecords)表服务控制类
 * 
 * @author makejava
 * @since 2023-11-18 19:20:43
 */
@Slf4j
@Controller
@RequestMapping("consumption-records")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class ConsumptionRecordsController {

    private final ConsumptionRecordsService consumptionRecordsService;

    // 代码写到此区域

    //批量导入记录


    @GetMapping("manage")
    public String manage(Model model, HttpSession session) {
        model.addAttribute("user", session.getAttribute("user"));
        return "member/consumption-records";
    }
    
    /************************************* 请在上方编写新代码 **************************************/
    // 此区域为自动生成的通用代码 尽量不直接修改

    @PostMapping("save")
    @ResponseBody
    public RestResponse<Long> save(@RequestBody ConsumptionRecords model) {
        try {
            Long id = consumptionRecordsService.save(model);
            return RestResponse.success("成功", id);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failed(e.getMessage());
        }
    }

    @DeleteMapping("remove/{id}")
    @ResponseBody
    public RestResponse<String> remove(@PathVariable Long id) {
        try {
            consumptionRecordsService.remove(id);
            return RestResponse.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failed(e.getMessage());
        }
    }

    @PostMapping("update")
    @ResponseBody
    public RestResponse<String> update(@RequestBody ConsumptionRecords model) {
        if (model.getId() == null) {
            throw new RuntimeException("ID不能为空");
        }
        try {
            consumptionRecordsService.update(model);
            return RestResponse.success("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failed(e.getMessage());
        }
    }

    @GetMapping("get/{id}")
    @ResponseBody
    public RestResponse<ConsumptionRecords> get(Long id) {
        try {
            ConsumptionRecords model = consumptionRecordsService.get(id);
            return model == null ? RestResponse.failed("不存在") : RestResponse.success("成功", model);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failed(e.getMessage());
        }
    }


    public RestResponse<Long> count(ConsumptionRecordsQuery query) {
        try {
            Long count = consumptionRecordsService.count(query);
            return RestResponse.success("成功", count);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failed(e.getMessage());
        }
    }

    @GetMapping("list")
    @ResponseBody
    public RestResponse<List<ConsumptionRecords>> list(ConsumptionRecordsQuery query) {
        try {
            return consumptionRecordsService.list(query);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failed(e.getMessage());
        }
    }

    @GetMapping("page")
    @ResponseBody
    public RestResponse<List<ConsumptionRecords>> page(ConsumptionRecordsQuery query) {
        try {
            return consumptionRecordsService.page(query);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResponse.failed(e.getMessage());
        }
    }
}
